using System;
using Microsoft.SharePoint;

namespace SharePoint.DataMapper
{
	/// <summary>
	/// ItemContext class.
	/// </summary>
	public class ItemContext : ListContext
	{
		private readonly int _itemId;
		private SPListItem _item;

		/// <summary>
		/// Initializes a new instance of the <see cref="ItemContext"/> class.
		/// </summary>
		/// <param name="listUrl">The SPList URL.</param>
		/// <param name="itemId">The item id.</param>
		public ItemContext(string listUrl, int itemId) : base(listUrl)
		{
			_itemId = itemId;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ItemContext"/> class.
		/// </summary>
		/// <param name="webUrlOrId">The web URL or id.</param>
		/// <param name="listNameOrId">The list name or id.</param>
		/// <param name="itemId">The item id.</param>
		public ItemContext(string webUrlOrId, string listNameOrId, int itemId)
			: base(webUrlOrId, listNameOrId)
		{
			_itemId = itemId;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ItemContext"/> class.
		/// </summary>
		/// <param name="siteId">The site id.</param>
		/// <param name="webId">The web id.</param>
		/// <param name="listNameOrId">The list name or id.</param>
		/// <param name="itemId">The item id.</param>
		public ItemContext(Guid siteId, Guid webId, string listNameOrId, int itemId)
			: base(siteId, webId, listNameOrId)
		{
			_itemId = itemId;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ItemContext"/> class.
		/// </summary>
		/// <param name="site">The site.</param>
		/// <param name="web">The web.</param>
		/// <param name="listNameOrId">The list name or id.</param>
		/// <param name="itemId">The item id.</param>
		public ItemContext(SPSite site, SPWeb web, string listNameOrId, int itemId)
			: base(site, web, listNameOrId)
		{
			_itemId = itemId;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ItemContext"/> class.
		/// </summary>
		/// <param name="webContext">The web context.</param>
		/// <param name="listNameOrId">The list name or id.</param>
		/// <param name="itemId">The item id.</param>
		public ItemContext(WebContext webContext, string listNameOrId, int itemId)
			: base(webContext, listNameOrId)
		{
			_itemId = itemId;
		}

		/// <summary>
		/// Gets the item.
		/// </summary>
		/// <value>The item.</value>
		public SPListItem Item
		{
			get
			{
				if (_item == null && _itemId > 0)
				{
					_item = List.GetItemById(_itemId);
				}
				return _item;
			}
		}
	}
}